package word;

import com.itextpdf.text.Element;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;


/**
 * @author weixinxin 2024-01-24
 **/
public class pdf文件添加水印 {
    public static void main(String[] args) {
        String waterMark = "内部使用";
        String filePath ="D:\\Win10Driver\\OneDrive\\桌面\\上海市临港地区人才租房补贴申请表.docx";
        String outFilePath = "D:\\Win10Driver\\OneDrive\\桌面\\上海市临港地区人才租房补贴申请表222.docx";
        File file = new File("D:\\Win10Driver\\OneDrive\\桌面\\上海市临港地区人才租房补贴申请表.docx");
        File file2 = new File("D:\\Win10Driver\\OneDrive\\桌面\\上海市临港地区人才租房补贴申请表222.docx");

        addPdfWatermark(filePath, outFilePath, waterMark);
    }


    public static void addPdfWatermark(String inputFile, String outputFile, String waterMarkName) {
        try {
            // 水印的高和宽（参数可调节）
            int textH = 75;
            int textW = 170;
            // 间隔距离（参数可调节）
            int interval = 30;
            String owerPwd = "111";
            String userWpd = "222";
            PdfReader reader = new PdfReader(inputFile);
            PdfReader.unethicalreading = true;
            PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));
            //userWpd 文件加密（打开文件密码）；owerPwd 操作加密（文件操作密码）； permissions = 4 能打印不能复制
            //stamper.setEncryption(userWpd.getBytes(),owerPwd.getBytes(),4,false);
            BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
            PdfGState gs = new PdfGState();
            gs.setFillOpacity(0.2f);
            gs.setStrokeOpacity(0.4f);
            int total = reader.getNumberOfPages() + 1;
            JLabel label = new JLabel();
            label.setText(waterMarkName);
            PdfContentByte under;

            Rectangle pageRect = null;
            FontMetrics metrics;
            label.setText(waterMarkName);
            metrics = label.getFontMetrics(label.getFont());
            for (int i = 1; i < total; i++) {
                pageRect = reader.getPageSizeWithRotation(i);
                // 在内容上方加水印
                under = stamper.getOverContent(i);
                // 在内容下方加水印
                //under = stamper.getUnderContent(i);
                under.saveState();
                under.setGState(gs);
                under.beginText();
                under.setFontAndSize(base, 20);
                // 水印文字成30度角倾斜
                for (int height = interval + textH; height < pageRect.getHeight();
                     height = height + textH * 3) {
                    for (int width = interval + textW; width < pageRect.getWidth() + textW;
                         width = width + textW * 2) {
                        under.showTextAligned(Element.ALIGN_LEFT
                                , waterMarkName, width - textW,
                                height - textH, 30);
                    }
                }
                under.endText();
            }
            stamper.close();
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}